package com.thh.filesystem.controller;


import com.thh.filesystem.domain.response.FileSystemCode;
import com.thh.filesystem.domain.response.UploadFileResult;
import com.thh.filesystem.exception.ExceptionCast;
import com.thh.filesystem.model.response.CommonCode;
import com.thh.filesystem.model.response.ResponseResult;
import com.thh.filesystem.service.FileSystemService;
import com.thh.filesystem.uitl.DownloadUtil;
import com.thh.filesystem.uitl.FastDFSClient2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

/**
 * @author Administrator
 * @version 1.0
 **/
@Controller
@RequestMapping("/filesystem")
public class FileSystemController   {
    @Autowired
    FileSystemService fileSystemService;



    @PostMapping("/upload")
    public UploadFileResult upload(MultipartFile multipartFile, String filetag, String businesskey, String metadata) {

        return fileSystemService.upload(multipartFile, filetag, businesskey, metadata);
    }



    @GetMapping("/download")
    public UploadFileResult download(String path, HttpServletResponse response)  {
        fileSystemService.download(path,response);
        return new UploadFileResult(FileSystemCode.FS_DOWNLOAD_SUCCESS);
    }



    @GetMapping("/download2")
    public UploadFileResult upload2(String group_name, String remote_filename,HttpServletResponse response) {
        fileSystemService.download2(group_name,remote_filename,response);
        return new UploadFileResult(FileSystemCode.FS_DOWNLOAD_SUCCESS);
    }

    @GetMapping("/urlDownload")
    public UploadFileResult urlDownload(String url,HttpServletResponse response) {
        DownloadUtil.downURL(url,response,"下载的图片.png");
        return new UploadFileResult(FileSystemCode.FS_DOWNLOAD_SUCCESS);
    }
}
